package net.godbirds.market.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import net.godbirds.bean.Product;
import net.godbirds.bean.Store;
import net.godbirds.market.service.StoreService;
import net.godbirds.util.HttpUtils;


@Service
public class StoreServiceImpl implements StoreService{

	private static Logger logger = LoggerFactory.getLogger(StoreServiceImpl.class);
	@Value("${global.getway.url}")
	String getway;
	@Value("${global.server.url}")
	String server;
	@Override
	public Store getStoreById(Long storeId) {
		if(storeId == null) {
			 storeId = (long) 1;
		}
		String url = getway+server+"/store/get/"+storeId;
		String codeStr = HttpUtils.doGet(url);
		JSONObject jsonObject = JSON.parseObject(codeStr);
		String retcode = jsonObject.getString("retcode");
		String retmsg = jsonObject.getString("retmsg");
		String data = jsonObject.getString("data");
		
		logger.debug(">>> retcode="+retcode);
		logger.debug(">>> retmsg="+retmsg);
		logger.debug(">>> data="+data);
		
		if(data == null || "".equals(data)) {
			return null;
		}
		Store store = JSON.parseObject(data, Store.class);
		return store;
	}

	@Override
	public Store getStoreByUser(String userCode) {
		return null;
	}

	@Override
	public List<Product> getProductByStoreId(Long storeId) {
		// TODO Auto-generated method stub
		String url = getway+server+"/store/getAllProduct/"+storeId;
		String codeStr = HttpUtils.doGet(url);
		JSONObject jsonObject = JSON.parseObject(codeStr);
		String retcode = jsonObject.getString("retcode");
		String retmsg = jsonObject.getString("retmsg");
		String data = jsonObject.getString("data");
		
		logger.debug(">>> retcode="+retcode);
		logger.debug(">>> retmsg="+retmsg);
		logger.debug(">>> data="+data);
		
		if(data == null || "".equals(data)) {
			return null;
		}
		List<Product> products = JSON.parseArray(data, Product.class);
		return products;
	}

	@Override
	public void deleteProductByPrdId(Long prdId) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteAllProductById(Long StoreId) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pullProductById(Long prdId) {
		// TODO Auto-generated method stub
		
	}

}
